package com.pth.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.pth.dao.NoticeDAO;
import com.pth.model.Notice;

public class NoticeDAOImpl implements NoticeDAO {

	private HibernateTemplate hibernateTemplate;
	
	public void save(Notice notice) {

		hibernateTemplate.save(notice);
		
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@SuppressWarnings("unchecked")
	public List<Notice> getnew9N() {

		final String hql="from Notice order by npublishtime desc";
		
		List<Notice> list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0) throws HibernateException,
					SQLException {

				return arg0.createQuery(hql).setFirstResult(0).setMaxResults(8).list();
				
			}
		});
		
		return list;
	}

	public Notice getNoticeByid(int id) {

		return hibernateTemplate.get(Notice.class, id);
		
	}

	@SuppressWarnings("unchecked")
	public List<Notice> getAllN(final int pagesize,final int offset) {

		final String hql="from Notice n order by n.npublishtime desc";
		List list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {

				return arg0.createQuery(hql).setFirstResult(offset)
					.setMaxResults(pagesize).list();
			}
		});
		
		return list;
		
	}

	public int getAllNum() {

		String hql="from Notice";
		return hibernateTemplate.find(hql).size();
	}

	public void delete(Notice notice) {

		hibernateTemplate.delete(notice);
		
	}

	public void update(Notice notice) {

		hibernateTemplate.update(notice);
	}

	@SuppressWarnings("unchecked")
	public List<Notice> getNoticesOrderByScannum(final int pagesize,final int offset) {

		final String hql="from Notice n order by n.scannum desc";
		List list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {

				return arg0.createQuery(hql).setFirstResult(offset)
					.setMaxResults(pagesize).list();
			}
		});
		
		return list;
		
		
		
		
	}

	@SuppressWarnings("unchecked")
	public List<Notice> getAllNotices() {

		return hibernateTemplate.find("from Notice");
		
	}

}
